package com.css.zfzx.fwsjcj.modules.statistics.fw.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.DivisionCodeConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.ProvinceConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.StructureConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.entity.FwAreaBuildingEntity;
import com.css.zfzx.fwsjcj.modules.statistics.fw.entity.FwAreaBuildingTemporaryEntity;
import com.css.zfzx.fwsjcj.modules.statistics.fw.exception.ExcelUploadException;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwAreaBuildingNativeReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwAreaBuildingReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwAreaBuildingTemporaryReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwDivisionAreaBuildingNativReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.service.FwAreaBuildingService;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.FwConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.util.Util;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.*;

@Slf4j
@Service
public class FwAreaBuildingServiceImpl implements FwAreaBuildingService {

    @Autowired
    FwAreaBuildingNativeReqository fwAreaBuildingNativReqository;

    @Autowired
    FwAreaBuildingReqository fwAreaBuildingReqository;

    @Autowired
    FwAreaBuildingTemporaryReqository fwAreaBuildingTemporaryReqository;

    @Autowired
    FwFileExcelVerifyHandler fwFileExcelVerifyHandler;

    @Autowired
    private FwDivisionAreaBuildingNativReqository fwDivisionAreaBuildingNativReqository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getAreaBuilding(Date date) {
        try {
            //总面积
            String areaIsOpen = fwAreaBuildingNativReqository.getAreaIsOpen("全国");//查询总面积修正值是否启用 area_is_open
            Double allArea = 0.0;
            if (areaIsOpen.equals("0")) {
                allArea = fwAreaBuildingNativReqository.getAllAmendatoryAreaByProvince("全国");//修正面积
            } else {
                allArea = fwAreaBuildingNativReqository.getAllActualAreaByProvince("全国");//实际面积
            }
            //allArea转换为平方米
            allArea = Util.mul(allArea);
            //总栋数
            String countIsOpen = fwAreaBuildingNativReqository.getCountIsOpen("全国");
            int allBuildingCount = 0;
            if (countIsOpen.equals("0")) {
                allBuildingCount = fwAreaBuildingNativReqository.getAllAmendatoryBuildingByProvince("全国");//修正栋数
            } else {
                allBuildingCount = fwAreaBuildingNativReqository.getAllActualBuildingByProvince("全国");//实际栋数
            }

            //砖混结构、砖木结构抽样面积及栋数
            List<SelectVo> zhList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("0");
            Double zhAllSamplingArea = 0.0;
            int zhAllSamplingBuilding = 0;
            for (SelectVo zh : zhList) {
                zhAllSamplingArea = zh.getSamplingArea();
                zhAllSamplingBuilding = zh.getSamplingBuilding();
            }
            //多层钢筋混凝土结构抽样面积及栋数
            List<SelectVo> dcList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("1");
            Double dcAllSamplingArea = 0.0;
            int dcAllSamplingBuilding = 0;
            for (SelectVo dc : dcList) {
                dcAllSamplingArea = dc.getSamplingArea();
                dcAllSamplingBuilding = dc.getSamplingBuilding();
            }
            //高层结构抽样面积及栋数
            List<SelectVo> gcList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("2");
            Double gcAllSamplingArea = 0.0;
            int gcAllSamplingBuilding = 0;
            for (SelectVo gc : gcList) {
                gcAllSamplingArea = gc.getSamplingArea();
                gcAllSamplingBuilding = gc.getSamplingBuilding();
            }
            //工业厂房结构抽样面积及栋数
            List<SelectVo> gyList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("3");
            Double gyAllSamplingArea = 0.0;
            int gyAllSamplingBuilding = 0;
            for (SelectVo gy : gyList) {
                gyAllSamplingArea = gy.getSamplingArea();
                gyAllSamplingBuilding = gy.getSamplingBuilding();
            }
            //空旷结构 抽样面积及栋数
            List<SelectVo> kkList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("4");
            Double kkAllSamplingArea = 0.0;
            int kkAllSamplingBuilding = 0;
            for (SelectVo kk : kkList) {
                kkAllSamplingArea = kk.getSamplingArea();
                kkAllSamplingBuilding = kk.getSamplingBuilding();
            }
            //土木结构 抽样面积及栋数
            List<SelectVo> tmList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("5");
            Double tmAllSamplingArea = 0.0;
            int tmAllSamplingBuilding = 0;
            for (SelectVo tm : tmList) {
                tmAllSamplingArea = tm.getSamplingArea();
                tmAllSamplingBuilding = tm.getSamplingBuilding();
            }
            //木结构 抽样面积及栋数
            List<SelectVo> mList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("6");
            Double mAllSamplingArea = 0.0;
            int mAllSamplingBuilding = 0;
            for (SelectVo m : mList) {
                mAllSamplingArea = m.getSamplingArea();
                mAllSamplingBuilding = m.getSamplingBuilding();
            }
            //石结构 抽样面积及栋数
            List<SelectVo> sList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("7");
            Double sAllSamplingArea = 0.0;
            int sAllSamplingBuilding = 0;
            for (SelectVo s : sList) {
                sAllSamplingArea = s.getSamplingArea();
                sAllSamplingBuilding = s.getSamplingBuilding();
            }
            //其他 抽样面积及栋数
            List<SelectVo> qtList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("8");
            Double qtAllSamplingArea = 0.0;
            int qtAllSamplingBuilding = 0;
            for (SelectVo qt : qtList) {
                qtAllSamplingArea = qt.getSamplingArea();
                qtAllSamplingBuilding = qt.getSamplingBuilding();
            }
            //砖木结构抽样面积及栋数
            List<SelectVo> zmList = fwAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType("9");
            Double zmAllSamplingArea = 0.0;
            int zmAllSamplingBuilding = 0;
            for (SelectVo zm : zmList) {
                zmAllSamplingArea = zm.getSamplingArea();
                zmAllSamplingBuilding = zm.getSamplingBuilding();
            }
            //全国抽样面积及抽样栋数
            List<SelectVo> allAreaAndBuildingCount = fwAreaBuildingNativReqository.getAllAreaAndBuildingCount();
            Double allSamplingArea = 0.0;
            int allSamplingBuilding = 0;
            for (SelectVo all : allAreaAndBuildingCount) {
                allSamplingArea = all.getSamplingArea();//平方米
                allSamplingBuilding = all.getSamplingBuilding();
            }
            //面积抽样率
            String areaSampling = Util.parsePercent(allSamplingArea, allArea);
            //栋数抽样率
            String buildingSampling = Util.parsePercent(allSamplingBuilding, allBuildingCount);

            //砖混结构抽样面积抽样率及栋数占比
            String zhSamplingArea = Util.parsePercent(zhAllSamplingArea, allSamplingArea);
            String zhSamplingBuilding = Util.parsePercent(zhAllSamplingBuilding, allSamplingBuilding);

            //多层钢筋混凝土结构 抽样面积抽样率及栋数占比
            String dcSamplingArea = Util.parsePercent(dcAllSamplingArea, allSamplingArea);
            String dcSamplingBuilding = Util.parsePercent(dcAllSamplingBuilding, allSamplingBuilding);

            //高层结构抽样面积抽样率及栋数占比
            String gcSamplingArea = Util.parsePercent(gcAllSamplingArea, allSamplingArea);
            String gcSamplingBuilding = Util.parsePercent(gcAllSamplingBuilding, allSamplingBuilding);

            //工业厂房结构抽样面积抽样率及栋数占比
            String gySamplingArea = Util.parsePercent(gyAllSamplingArea, allSamplingArea);
            String gySamplingBuilding = Util.parsePercent(gyAllSamplingBuilding, allSamplingBuilding);

            //空旷结构抽样面积抽样率及栋数占比
            String kkSamplingArea = Util.parsePercent(kkAllSamplingArea, allSamplingArea);
            String kkSamplingBuilding = Util.parsePercent(kkAllSamplingBuilding, allSamplingBuilding);

            //土木结构 抽样面积抽样率及栋数占比
            String tmSamplingArea = Util.parsePercent(tmAllSamplingArea, allSamplingArea);
            String tmSamplingBuilding = Util.parsePercent(tmAllSamplingBuilding, allSamplingBuilding);

            //木结构 抽样面积抽样率及栋数占比
            String mSamplingArea = Util.parsePercent(mAllSamplingArea, allSamplingArea);
            String mSamplingBuilding = Util.parsePercent(mAllSamplingBuilding, allSamplingBuilding);

            //石结构 抽样面积抽样率及栋数占比
            String sSamplingArea = Util.parsePercent(sAllSamplingArea, allSamplingArea);
            String sSamplingBuilding = Util.parsePercent(sAllSamplingBuilding, allSamplingBuilding);

            //其他结构 抽样面积抽样率及栋数占比
            String qtSamplingArea = Util.parsePercent(qtAllSamplingArea, allSamplingArea);
            String qtSamplingBuilding = Util.parsePercent(qtAllSamplingBuilding, allSamplingBuilding);

            //砖木结构抽样面积抽样率及栋数占比
            String zmSamplingArea = Util.parsePercent(zmAllSamplingArea, allSamplingArea);
            String zmSamplingBuilding = Util.parsePercent(zmAllSamplingBuilding, allSamplingBuilding);

            log.info("地图面积和栋数查询成功--------");

            String userId = PlatformSessionUtils.getUserId();
            //全国相关数据
            FwAreaBuildingTemporaryEntity fwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            fwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            fwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            fwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            fwAreaBuildingTemporaryEntity.setDivisionLevel("0");
            fwAreaBuildingTemporaryEntity.setAreaData(Util.div(allSamplingArea));//省（直辖市）面积  保存为万平方米
            fwAreaBuildingTemporaryEntity.setAreaSampling(areaSampling); //面积抽样率
            fwAreaBuildingTemporaryEntity.setCountData(allSamplingBuilding);//省（直辖市）栋数
            fwAreaBuildingTemporaryEntity.setCountSampling(buildingSampling);//栋数抽样率
            fwAreaBuildingTemporaryEntity.setIsStructure("0");//是否有结构类型
            fwAreaBuildingTemporaryEntity.setDelFlag("0");
            fwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            fwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(fwAreaBuildingTemporaryEntity);
            log.info("全国相关数据保存成功--------");
            //保存砖混结构
            FwAreaBuildingTemporaryEntity zhFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            zhFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            zhFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            zhFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            zhFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            zhFwAreaBuildingTemporaryEntity.setStructureName("砖混结构");//结构类型
            zhFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(zhAllSamplingArea));//结构面积
            zhFwAreaBuildingTemporaryEntity.setStructureAreaProportion(zhSamplingArea);//面积占比
            zhFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(zhAllSamplingBuilding));//结构栋数
            zhFwAreaBuildingTemporaryEntity.setStructureCountProportion(zhSamplingBuilding);//栋数占比
            zhFwAreaBuildingTemporaryEntity.setDelFlag("0");
            zhFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            zhFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(zhFwAreaBuildingTemporaryEntity);
            log.info("砖混结构相关数据保存成功--------");
            //保存多层钢筋混凝土结构
            FwAreaBuildingTemporaryEntity dcFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            dcFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            dcFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            dcFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            dcFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            dcFwAreaBuildingTemporaryEntity.setStructureName("多层钢筋混凝土结构");//结构类型
            dcFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(dcAllSamplingArea));//结构面积
            dcFwAreaBuildingTemporaryEntity.setStructureAreaProportion(dcSamplingArea);//面积占比
            dcFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(dcAllSamplingBuilding));//结构栋数
            dcFwAreaBuildingTemporaryEntity.setStructureCountProportion(dcSamplingBuilding);//栋数占比
            dcFwAreaBuildingTemporaryEntity.setDelFlag("0");
            dcFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            dcFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(dcFwAreaBuildingTemporaryEntity);
            log.info("多层钢筋混凝土结构相关数据保存成功--------");
            //高层结构抽样面积及栋数
            FwAreaBuildingTemporaryEntity gcFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            gcFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            gcFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            gcFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            gcFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            gcFwAreaBuildingTemporaryEntity.setStructureName("高层结构");//结构类型
            gcFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(gcAllSamplingArea));//结构面积
            gcFwAreaBuildingTemporaryEntity.setStructureAreaProportion(gcSamplingArea);//面积占比
            gcFwAreaBuildingTemporaryEntity.setStructureCountData(gcAllSamplingBuilding);//结构栋数
            gcFwAreaBuildingTemporaryEntity.setStructureCountProportion(gcSamplingBuilding);//栋数占比
            gcFwAreaBuildingTemporaryEntity.setDelFlag("0");
            gcFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            gcFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(gcFwAreaBuildingTemporaryEntity);
            log.info("高层结构相关数据保存成功--------");
            //工业厂房结构
            FwAreaBuildingTemporaryEntity gyFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            gyFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            gyFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            gyFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            gyFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            gyFwAreaBuildingTemporaryEntity.setStructureName("工业厂房结构");//结构类型
            gyFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(gyAllSamplingArea));//结构面积
            gyFwAreaBuildingTemporaryEntity.setStructureAreaProportion(gySamplingArea);//面积占比
            gyFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(gyAllSamplingBuilding));//结构栋数
            gyFwAreaBuildingTemporaryEntity.setStructureCountProportion(gySamplingBuilding);//栋数占比
            gyFwAreaBuildingTemporaryEntity.setDelFlag("0");
            gyFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            gyFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(gyFwAreaBuildingTemporaryEntity);
            log.info("工业厂房结构相关数据保存成功--------");
            //空旷结构
            FwAreaBuildingTemporaryEntity kkFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            kkFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            kkFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            kkFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            kkFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            kkFwAreaBuildingTemporaryEntity.setStructureName("空旷结构");//结构类型
            kkFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(kkAllSamplingArea));//结构面积
            kkFwAreaBuildingTemporaryEntity.setStructureAreaProportion(kkSamplingArea);//面积占比
            kkFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(kkAllSamplingBuilding));//结构栋数
            kkFwAreaBuildingTemporaryEntity.setStructureCountProportion(kkSamplingBuilding);//栋数占比
            kkFwAreaBuildingTemporaryEntity.setDelFlag("0");
            kkFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            kkFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(kkFwAreaBuildingTemporaryEntity);
            log.info("空旷结构相关数据保存成功--------");
            //土木结构
            FwAreaBuildingTemporaryEntity tmFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            tmFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            tmFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            tmFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            tmFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            tmFwAreaBuildingTemporaryEntity.setStructureName("土木结构");//结构类型
            tmFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(tmAllSamplingArea));//结构面积
            tmFwAreaBuildingTemporaryEntity.setStructureAreaProportion(tmSamplingArea);//面积占比
            tmFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(tmAllSamplingBuilding));//结构栋数
            tmFwAreaBuildingTemporaryEntity.setStructureCountProportion(tmSamplingBuilding);//栋数占比
            tmFwAreaBuildingTemporaryEntity.setDelFlag("0");
            tmFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            tmFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(tmFwAreaBuildingTemporaryEntity);
            log.info("土木结构相关数据保存成功--------");
            //木结构
            FwAreaBuildingTemporaryEntity mFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            mFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            mFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            mFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            mFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            mFwAreaBuildingTemporaryEntity.setStructureName("木结构");//结构类型
            mFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(mAllSamplingArea));//结构面积
            mFwAreaBuildingTemporaryEntity.setStructureAreaProportion(mSamplingArea);//面积占比
            mFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(mAllSamplingBuilding));//结构栋数
            mFwAreaBuildingTemporaryEntity.setStructureCountProportion(mSamplingBuilding);//栋数占比
            mFwAreaBuildingTemporaryEntity.setDelFlag("0");
            mFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            mFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(mFwAreaBuildingTemporaryEntity);
            log.info("木结构相关数据保存成功--------");
            //石结构
            FwAreaBuildingTemporaryEntity sFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            sFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            sFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            sFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            sFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            sFwAreaBuildingTemporaryEntity.setStructureName("石结构");//结构类型
            sFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(sAllSamplingArea));//结构面积
            sFwAreaBuildingTemporaryEntity.setStructureAreaProportion(sSamplingArea);//面积占比
            sFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(sAllSamplingBuilding));//结构栋数
            sFwAreaBuildingTemporaryEntity.setStructureCountProportion(sSamplingBuilding);//栋数占比
            sFwAreaBuildingTemporaryEntity.setDelFlag("0");
            sFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            sFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(sFwAreaBuildingTemporaryEntity);
            log.info("石结构相关数据保存成功--------");
            //其他
            FwAreaBuildingTemporaryEntity qtFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            qtFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            qtFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            qtFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            qtFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            qtFwAreaBuildingTemporaryEntity.setStructureName("其他");//结构类型
            qtFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(qtAllSamplingArea));//结构面积
            qtFwAreaBuildingTemporaryEntity.setStructureAreaProportion(qtSamplingArea);//面积占比
            qtFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(qtAllSamplingBuilding));//结构栋数
            qtFwAreaBuildingTemporaryEntity.setStructureCountProportion(qtSamplingBuilding);//栋数占比
            qtFwAreaBuildingTemporaryEntity.setDelFlag("0");
            qtFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            qtFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(qtFwAreaBuildingTemporaryEntity);
            log.info("其他相关数据保存成功--------");
            //保存砖木结构
            FwAreaBuildingTemporaryEntity zmFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
            zmFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
            zmFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
            zmFwAreaBuildingTemporaryEntity.setDistrictName("全国");//省（直辖市）名称
            zmFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
            zmFwAreaBuildingTemporaryEntity.setStructureName("砖木结构");//结构类型
            zmFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(zmAllSamplingArea));//结构面积
            zmFwAreaBuildingTemporaryEntity.setStructureAreaProportion(zmSamplingArea);//面积占比
            zmFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(zmAllSamplingBuilding));//结构栋数
            zmFwAreaBuildingTemporaryEntity.setStructureCountProportion(zmSamplingBuilding);//栋数占比
            zmFwAreaBuildingTemporaryEntity.setDelFlag("0");
            zmFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
            zmFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
            fwAreaBuildingTemporaryReqository.save(zmFwAreaBuildingTemporaryEntity);
            log.info("砖木结构相关数据保存成功--------");

            //查询各省数据
            FwConstants fwConstants = new FwConstants();
            DivisionCodeConstants dConstants = new DivisionCodeConstants(); //!!!该两个常量类中的顺序需保持一致
            Field[] fields = fwConstants.getClass().getDeclaredFields();
            Field[] fieldl = dConstants.getClass().getDeclaredFields();//获取省名称对应的省编码
            for (int i = 0; i < fields.length; i++) {
                String province = fields[i].get(fwConstants).toString();
                String division = fieldl[i].get(dConstants).toString();
                log.info("开始查询 " + province + " 数据--------");
                //各省总面积
                String areaIsOpen2 = fwAreaBuildingNativReqository.getAreaIsOpen(province);//查询总面积修正值是否启用 area_is_open
                Double proAllArea = 0.0;
                if (areaIsOpen2.equals("0")) {
                    proAllArea = fwAreaBuildingNativReqository.getAllAmendatoryAreaByProvince(province);//修正面积
                } else {
                    proAllArea = fwAreaBuildingNativReqository.getAllActualAreaByProvince(province);//实际面积
                }
                //转为平方米
                proAllArea = Util.mul(proAllArea);

                //各省总栋数
                String countIsOpen2 = fwAreaBuildingNativReqository.getCountIsOpen(province);//查询总栋数修正值是否启用 count_is_open
                int proAllBuilding = 0;
                if (countIsOpen2.equals("0")) {
                    proAllBuilding = fwAreaBuildingNativReqository.getAllAmendatoryBuildingByProvince(province);//修正栋数
                } else {
                    proAllBuilding = fwAreaBuildingNativReqository.getAllActualBuildingByProvince(province);//实际栋数
                }
                //各省抽样面积
                List<SelectVo> lists = fwAreaBuildingNativReqository.getProSamplingAreaAndBuilding(province);
                Double proSamplingArea = 0.0;
                int proSamplingBuilding = 0;
                for (SelectVo proSamplingAreaAndBuilding : lists) {
                    proSamplingArea = proSamplingAreaAndBuilding.getSamplingArea();
                    proSamplingBuilding = proSamplingAreaAndBuilding.getSamplingBuilding();
                }

                //各省面积抽样率
                String proAreaSampling = Util.parsePercent(proSamplingArea, proAllArea);
                //各省栋数抽样率
                String proBuildingSampling = Util.parsePercent(proSamplingBuilding, proAllBuilding);

                FwAreaBuildingTemporaryEntity proFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                proFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                proFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                proFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                proFwAreaBuildingTemporaryEntity.setDivisionCode(division);//省编码
                proFwAreaBuildingTemporaryEntity.setDivisionLevel("1");
                proFwAreaBuildingTemporaryEntity.setAreaData(Util.div(proSamplingArea));//省（直辖市）面积  万平方米
                proFwAreaBuildingTemporaryEntity.setAreaSampling(proAreaSampling);//面积抽样率
                proFwAreaBuildingTemporaryEntity.setCountData((int) Math.round(proSamplingBuilding));//省（直辖市）栋数
                proFwAreaBuildingTemporaryEntity.setCountSampling(proBuildingSampling);//栋数抽样率
                proFwAreaBuildingTemporaryEntity.setIsStructure("0");//是否有结构类型
                proFwAreaBuildingTemporaryEntity.setDelFlag("0");
                proFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                proFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(proFwAreaBuildingTemporaryEntity);
                log.info("---" + province + "数据保存成功----------");
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取数据失败");
            return "error";
        }
    }

    @Override
    public List<SelectChinaAndProvinceVo> getChinaData(){
        List<SelectChinaAndProvinceVo> chinaData = fwAreaBuildingNativReqository.getChinaData("0");
        int size = chinaData.size();
        if (size < 1) {
            SelectChinaAndProvinceVo selectChinaAndProvinceVo = new SelectChinaAndProvinceVo();
            selectChinaAndProvinceVo.setId(UUIDGenerator.getUUID());
            selectChinaAndProvinceVo.setDataTime(null);//数据日期
            selectChinaAndProvinceVo.setDistrictName("全国");//省（直辖市）名称
            selectChinaAndProvinceVo.setAreaData(0.0);//省（直辖市）面积
            selectChinaAndProvinceVo.setAreaSampling("0%"); //面积抽样率
            selectChinaAndProvinceVo.setCountData(0);//省（直辖市）栋数
            selectChinaAndProvinceVo.setCountSampling("0%");//栋数抽样率
            chinaData.add(selectChinaAndProvinceVo);
        }
        return chinaData;
    }

    @Override
    public List<SelectStructureVo> getStructureData() throws IllegalAccessException {
        List<SelectStructureVo> structureData = fwAreaBuildingNativReqository.getStructureData();
        ArrayList<Object> arrayList = new ArrayList<>();
        for(SelectStructureVo structure:structureData){
            String structureName = structure.getStructureName();
            arrayList.add(structureName);
        }
        StructureConstants structureConstants = new StructureConstants();
        Field[] fields = structureConstants.getClass().getDeclaredFields();
        for (Field field : fields) {
            String structure = field.get(structureConstants).toString();
            boolean contains = arrayList.contains(structure);
            if(!contains){
                SelectStructureVo selectStructureVo = new SelectStructureVo();
                selectStructureVo.setId(UUIDGenerator.getUUID());
                selectStructureVo.setDataTime(null);//数据日期
                selectStructureVo.setStructureName(structure);//结构类型
                selectStructureVo.setStructureAreaData(0.0);//结构面积
                selectStructureVo.setStructureAreaProportion("0%");//面积占比
                selectStructureVo.setStructureCountData(0);//结构栋数
                selectStructureVo.setStructureCountProportion("0%");//栋数占比
                structureData.add(selectStructureVo);
            }
        }
        return structureData;
    }

    @Override
    public List<SelectChinaAndProvinceVo> getProvinceData() throws IllegalAccessException {
        List<SelectChinaAndProvinceVo> provinceData = fwAreaBuildingNativReqository.getProvinceData("1");
        ArrayList<Object> arrayList = new ArrayList<>();
        for(SelectChinaAndProvinceVo provinceVo:provinceData){
            String districtName = provinceVo.getDistrictName();
            arrayList.add(districtName);
        }
        FwConstants fwConstants = new FwConstants();
        Field[] fields = fwConstants.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fw = field.get(fwConstants).toString();
            boolean contains = arrayList.contains(fw);
            if(!contains){
                SelectChinaAndProvinceVo selectChinaAndProvinceVo = new SelectChinaAndProvinceVo();
                selectChinaAndProvinceVo.setId(UUIDGenerator.getUUID());
                selectChinaAndProvinceVo.setDataTime(null);//数据日期
                selectChinaAndProvinceVo.setDistrictName(fw);//省（直辖市）名称
                selectChinaAndProvinceVo.setAreaData(0.0);//省（直辖市）面积
                selectChinaAndProvinceVo.setAreaSampling("0%");//面积抽样率
                selectChinaAndProvinceVo.setCountData(0);//省（直辖市）栋数
                selectChinaAndProvinceVo.setCountSampling("0%");//栋数抽样率
                provinceData.add(selectChinaAndProvinceVo);
            }
        }
        return provinceData;
    }

    @Override
    public List<ProvinceParams> queryProvinceFwData() throws IllegalAccessException {
        List<ProvinceParams> list = fwAreaBuildingNativReqository.queryProvinceFwData();
        ArrayList<Object> arrayList = new ArrayList<>();
        for(ProvinceParams provinceParams:list){
            String districtName = provinceParams.getDistrictName();
            arrayList.add(districtName);
        }
        FwConstants fwConstants = new FwConstants();
        Field[] fields = fwConstants.getClass().getDeclaredFields();

        for (Field field : fields) {
            String fw = field.get(fwConstants).toString();
            boolean contains = arrayList.contains(fw);
            if(!contains){
                ProvinceParams provinceParams = new ProvinceParams();
                provinceParams.setDistrictName(fw);//地区名称
                provinceParams.setAllBuildingArea(0.0);//总面积实际值（万平方米）
                provinceParams.setAmendatoryAllBuildingArea(0.0);//总面积修正值（万平方米）
                provinceParams.setAreaIsOpen("");//总面积修正值是否启用
                provinceParams.setAllBuildingCount(0);//总栋数实际值
                provinceParams.setAmendatoryAllBuildingCount(0);//总栋数修正值
                provinceParams.setCountIsOpen("");//总栋数修正值是否启用
                list.add(provinceParams);
            }
        }
        return list;
    }

    @Override
    public List<SelectChinaAndProvinceVo> getChinaProvinceData() throws IllegalAccessException {
        List<SelectChinaAndProvinceVo> chinaProvinceData = fwAreaBuildingNativReqository.getChinaProvinceData("1");
        ArrayList<Object> arrayList = new ArrayList<>();
        ProvinceConstants provinceConstants = new ProvinceConstants();
        Field[] fields = provinceConstants.getClass().getDeclaredFields();
        //判断已查询的结果集中的districtName,给name重新赋值
        for(int i=0; i<chinaProvinceData.size();i++){
            String districtName = chinaProvinceData.get(i).getDistrictName();
            arrayList.add(districtName);
            for (Field field2 : fields) {
                String province = field2.get(provinceConstants).toString();
                if (districtName.contains(province)) {
                    chinaProvinceData.get(i).setName(province);
                }
            }
        }
        //是否有31条数据,没有则新增一条
        for (Field field : fields) {
            String province = field.get(provinceConstants).toString();
            String str = arrayList.toString();
            SelectChinaAndProvinceVo selectChinaAndProvinceVo = new SelectChinaAndProvinceVo();
            if(!str.contains(province)){
                selectChinaAndProvinceVo.setId(UUIDGenerator.getUUID());
                selectChinaAndProvinceVo.setDataTime(null);//数据日期
                selectChinaAndProvinceVo.setName(province);
                selectChinaAndProvinceVo.setValue(0.0);
                selectChinaAndProvinceVo.setAreaSampling("0%");//面积抽样率
                selectChinaAndProvinceVo.setCountData(0);//省（直辖市）栋数
                selectChinaAndProvinceVo.setCountSampling("0%");//栋数抽样率
                chinaProvinceData.add(selectChinaAndProvinceVo);
            }
        }
        return chinaProvinceData;
    }

    @Override
    @Transactional
    public String updateDataByDistrict(ProvinceParams fwParams) {
        String districtName = fwParams.getDistrictName();
        Double allBuildingArea = fwParams.getAllBuildingArea();
        Double amendatoryAllBuildingArea = fwParams.getAmendatoryAllBuildingArea();
        String areaIsOpen = fwParams.getAreaIsOpen();
        int allBuildingCount = fwParams.getAllBuildingCount();
        int amendatoryAllBuildingCount = fwParams.getAmendatoryAllBuildingCount();
        String countIsOpen = fwParams.getCountIsOpen();
        String userId = PlatformSessionUtils.getUserId();
        Date date = new Date();
        //判断库中是否有该省的数据,有则更新数据,没有则新增一条数据
        String divisionLevel = "";
        String divisionCode = "";
        if(districtName.equals("全国")){
            divisionLevel = "0";
            divisionCode = "999999";
        }else{
            divisionLevel = "1";
            divisionCode = fwDivisionAreaBuildingNativReqository.getDivisionCode(districtName);
        }
        int count = fwAreaBuildingReqository.queryFwDateByDistrict(districtName,divisionLevel);
        if(count == 0){
            FwAreaBuildingEntity fwAreaBuildingEntity = new FwAreaBuildingEntity();
            fwAreaBuildingEntity.setId(UUIDGenerator.getUUID());
            fwAreaBuildingEntity.setDistrictName(districtName);
            fwAreaBuildingEntity.setDivisionCode(divisionCode);
            fwAreaBuildingEntity.setDivisionLevel(divisionLevel);
            fwAreaBuildingEntity.setAllBuildingArea(allBuildingArea);
            fwAreaBuildingEntity.setAmendatoryAllBuildingArea(amendatoryAllBuildingArea);
            fwAreaBuildingEntity.setAreaIsOpen(areaIsOpen);
            fwAreaBuildingEntity.setAllBuildingCount(allBuildingCount);
            fwAreaBuildingEntity.setAmendatoryAllBuildingCount(amendatoryAllBuildingCount);
            fwAreaBuildingEntity.setCountIsOpen(countIsOpen);
            fwAreaBuildingEntity.setDelFlag("0");
            fwAreaBuildingEntity.setCreatorId(userId);
            fwAreaBuildingEntity.setCreateTime(date);
            fwAreaBuildingReqository.save(fwAreaBuildingEntity);
        }else{
            fwAreaBuildingReqository.updateDataByDistrict(districtName, allBuildingArea, amendatoryAllBuildingArea, areaIsOpen,
                    allBuildingCount, amendatoryAllBuildingCount, countIsOpen, userId, date);
        }
        return "修改成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setImportFields(new String[]{"省名称（包括全国、省、直辖市、自治区）", "市名称", "总建筑面积（万平方米）", "总建筑面积修正值（万平方米）", "总建筑面积修正值是否启用", "总栋数（栋）", "总栋数修正值（栋）", "总栋数修正值是否启用"});
        params.setNeedVerify(true);
        params.setVerifyHandler(fwFileExcelVerifyHandler);
        List<FwFileExcelVo> excelData;
        try {
            InputStream inputStream = file.getInputStream();
            ExcelImportResult<FwFileExcelVo> result = ExcelImportUtil.importExcelMore(inputStream, FwFileExcelVo.class, params);
            excelData = result.getList();
            List<FwFileExcelVo> failList = result.getFailList();
            if (PlatformObjectUtils.isNotEmpty(failList)) {
                StringBuilder errorMsg = new StringBuilder("导入失败");
                for (FwFileExcelVo fwFileExcelVo : failList) {
                    errorMsg.append("<br>").append("第").append(fwFileExcelVo.getRowNum() + 1).append("行： ").append(fwFileExcelVo.getErrorMsg());
                }
                throw new ExcelUploadException(errorMsg.toString());
            }

            if (PlatformObjectUtils.isNotEmpty(excelData)) {
                int count = 0;
                StringBuilder errorMsg = new StringBuilder("导入失败：");
                for (FwFileExcelVo fwFileExcelVo : excelData) {
                    String districtName = fwFileExcelVo.getDistrictName();
                    String belongtoCity = fwFileExcelVo.getBelongtoCity();
                    //根据省名称  市名称  查询库中是否有该省 市的数据,如果有则给出提示
                    count = fwAreaBuildingNativReqository.getIsData(districtName, belongtoCity);
                    if (count != 0) {
                        errorMsg.append("<br>").append("第").append(fwFileExcelVo.getRowNum() + 1).append("行： ").append(districtName + " " + belongtoCity + " 数据库中已有数据,请删除excel表中" + districtName + " " + belongtoCity + "的数据之后再导入!");
                    }
                }
                if (count != 0) {
                    throw new ExcelUploadException(errorMsg.toString());
                }

            }
            FwAreaBuildingEntity fwAreaBuildingEntity;
            if (PlatformObjectUtils.isNotEmpty(excelData)) {
                for (FwFileExcelVo fwData : excelData) {
                    fwAreaBuildingEntity = new FwAreaBuildingEntity();
                    fwAreaBuildingEntity.setId(UUIDGenerator.getUUID());
                    String districtName = fwData.getDistrictName();
                    String belongtoCity = fwData.getBelongtoCity();
                    log.info("解析excel--开始保存 "+districtName+" "+belongtoCity+" 数据------------------");
                    fwAreaBuildingEntity.setDistrictName(districtName);//省名称
                    fwAreaBuildingEntity.setBelongtoCity(belongtoCity);//市名称
                    //获取编码
                    String divisionCode = "";
                    String divisionName = "";
                    if (districtName.equals("全国")) {
                        divisionCode = "999999";
                    } else {
                        if (PlatformObjectUtils.isNotEmpty(belongtoCity)) {
                            divisionName = belongtoCity;
                        } else {
                            divisionName = districtName;
                        }
                        divisionCode = fwDivisionAreaBuildingNativReqository.getDivisionCode(divisionName);
                    }
                    fwAreaBuildingEntity.setDivisionCode(divisionCode);
                    //获取级别
                    String divisionLevel = "";
                    if (districtName.equals("全国")) {
                        divisionLevel = "0";
                    } else {
                        if (PlatformObjectUtils.isEmpty(belongtoCity)) {
                            divisionLevel = "1";
                        } else {
                            divisionLevel = "2";
                        }
                    }
                    fwAreaBuildingEntity.setDivisionLevel(divisionLevel);
                    fwAreaBuildingEntity.setAllBuildingArea(Double.parseDouble(fwData.getAllBuildingArea()));
                    fwAreaBuildingEntity.setAmendatoryAllBuildingArea(Double.parseDouble(fwData.getAmendatoryAllBuildingArea()));
                    String areaIsOpen = "";
                    if (fwData.getAreaIsOpen().equals("是")) {
                        areaIsOpen = "0";
                    } else {
                        areaIsOpen = "1";
                    }
                    fwAreaBuildingEntity.setAreaIsOpen(areaIsOpen);
                    fwAreaBuildingEntity.setAllBuildingCount(Integer.parseInt(fwData.getAllBuildingCount()));
                    fwAreaBuildingEntity.setAmendatoryAllBuildingCount(Integer.parseInt(fwData.getAmendatoryAllBuildingCount()));
                    String countIsOpen = "";
                    if (fwData.getCountIsOpen().equals("是")) {
                        countIsOpen = "0";
                    } else {
                        countIsOpen = "1";
                    }
                    fwAreaBuildingEntity.setCountIsOpen(countIsOpen);
                    fwAreaBuildingEntity.setDelFlag("0");
                    fwAreaBuildingEntity.setCreatorId(PlatformSessionUtils.getUserId());
                    fwAreaBuildingEntity.setCreateTime(new Date());
                    fwAreaBuildingReqository.save(fwAreaBuildingEntity);
                    log.info("解析excel--"+districtName +" "+belongtoCity+"数据保存成功-----------");
                }
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导入数据失败");
            throw new RuntimeException(e.getMessage());
        }
    }




}
